import router from "./router";
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import pinia from "./store";
import useUserStore from "./store/modules/user";
let userStore = useUserStore(pinia)
nprogress.configure({showSpinner:false})
router.beforeEach((to:any, from:any, next:any) => {
    document.title = to.meta.title
    nprogress.start()
    let token = userStore.token
    let username = userStore.username
    if(token){
    if(to.path!='login'){
        if(username){
            next()
        }else{
           userStore.userInfo().then(()=>{
            next()
        }).catch(async ()=>{
            await userStore.userLogout()
            next({path:'/login',query:{redirect:to.path}})
        })
        }
    }else{
        next({path:'/'})
    }
    }else{
        if(to.path=='/login'){
            next()
        }else{
            next({path:'/login',query:{redirect:to.path}})
        }
    }
});

router.afterEach((to:any, from:any) => {
    nprogress.done()
});